/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jobportal.Dbconn;
import jobportal.EmailSender;

/**
 *
 * @author project
 */
public class ManageJobs extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            Connection con = null;
            Statement st = null;
            ResultSet rs;
            RequestDispatcher rd = request.getRequestDispatcher("manage_jobs.jsp");
            String sql = "";
            String email = "";
       try{
           con = Dbconn.getConn();
            st = con.createStatement();
            String str[] = request.getParameterValues("cmcheck[]");
            String jobid = "", jobemail = "";
            for (int i = 0; i < str.length; i++) {
                if (i == 0) {
                    jobid = "'" + str[i] + "'";
                } else {
                    jobid += ",'" + str[i] + "'";
                }

            }
            
            rs = st.executeQuery("select subject,content from emailmessage where id=16");
            String subject = "", content = "";
            if (rs.next()) {
                subject = rs.getString(1);
                content = rs.getString(2);
            }

            out.println("jobid=" + jobid);

            if (request.getParameter("sendemail") != null && request.getParameter("sendemail").equals("Send Email")) {
            } else if (request.getParameter("suspend") != null && request.getParameter("suspend").equals("Inactive")) {
                sql = "update jobs set status='Inactive' where jobid in(" + jobid + ")";
                st.executeUpdate(sql);
                out.println("ajay");
            } else if (request.getParameter("active") != null && request.getParameter("active").equals("Active")) {
                sql = "update jobs set status='Active' where jobid in(" + jobid + ")";
                st.executeUpdate(sql);
                rs = st.executeQuery("select jobid,jobtitle,compname from jobs where jobs.compid=company.compid and jobid in(" + jobid + ") and status is null");
                String content1 = "";
                while (rs.next()) {
                    content1 = content;
                    content1=content1.replace("%jobtitle%", rs.getString("jobtitle"));
                    content1=content1.replace("%compname%", rs.getString("compname"));
                    EmailSender.sendMail(email, subject, content1);
                }
            } else if (request.getParameter("delete") != null && request.getParameter("delete").equals("Delete")) {
                sql = "delete from jobs where jobid in(" + jobid + ")";
                st.executeUpdate(sql);
            }
            rd.forward(request, response);
        } catch (Exception e) {
            
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
